package com.sp.scmdb.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.DepartmentBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.Department;
import com.sp.scmdb.service.DepartmentService;

@Controller("departmentAction")
@Scope("prototype")
public class DepartmentAction extends BaseAction<DepartmentBean> {

	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(DepartmentAction.class);

	private DepartmentService departmentService;

	@Autowired
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public String findAll() {
		try {
			List<Department> depList = departmentService.queryAll();
			entity.setAllList(depList);
			entity.setList(depList.subList(entity.getCurrentRow(), entity.getLastRow()));

			this.button("Super.Popup(5,'添加班组','/SCMDB/departmentAction!addInitDep.action','',400,120)",
					"Super.editData('/SCMDB/departmentAction!findDep.action?id=',400,120)",
					"Super.delData('/SCMDB/departmentAction!delDep.action?id=')");
		} catch (RuntimeException e) {
			log.error("findAll fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("findAll fail!reason:" + e.getMessage());
			return "error";
		}
		return SUCCESS;
	}

	public String addInitDep() {
		return "addSucc";
	}

	/**
	 * 添加部门
	 */
	public String addDep() {
		try {
			UserBean user = (UserBean) session.get("user");
			String result = departmentService.newDep(entity, user);
			if (log.isInfoEnabled()) {
				log.info("add suncce");
			}
			messageSession(result, "班组");
		} catch (RuntimeException e) {
			log.error("addDep fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("addDep fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}

	/**
	 * 跳转至部门修改页面
	 */
	public String findDep() {
		try {
			String depId = request.getParameter("id");
			Department department = departmentService.findID(depId);
			if (null != department) {
				entity.setId(String.valueOf(department.getId()));
				entity.setDeptName(department.getDeptName());
			} else {
				// 如果部门不存在，返回到查询页面
				return "commitSucc";
			}
		} catch (RuntimeException e) {
			log.error("findDep fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("findDep fail!reason:" + e.getMessage());
			return "error";
		}
		return "editSucc";
	}

	/**
	 * 更新部门
	 */
	public String editDep() {
		try {
			UserBean user = (UserBean) session.get("user");
			String result = departmentService.editDep(entity, user);
			if (log.isInfoEnabled()) {
				log.info("edit suncce");
			}
			messageSession(result, "班组");
		} catch (RuntimeException e) {
			log.error("editDep fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("editDep fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}

	/**
	 * 删除部门
	 */
	public String delDep() {
		try {
			UserBean user = (UserBean) session.get("user");
			String depId = request.getParameter("id");
			if (!StringUtils.isEmpty(depId)) {
				String result = departmentService.delDep(depId.split(","), user);
				if ("1".equals(result)) {
					messageSession(result, "班组");
				} else if ("10".equals(result)) {
					messageSession("0", "该班组有所属人员，不能删除！");
				}
			}
		} catch (RuntimeException e) {
			log.error("delDep fail!reason:" + e.getMessage());
			return "fail";
		} catch (Exception e) {
			log.error("delDep fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}
}
